package spring.cloud.tasks.api.analyzer;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import spring.cloud.tasks.api.database.entity.StatisticsDto;
import spring.cloud.tasks.api.database.service.StatisticsDtoService;
import spring.cloud.tasks.api.domain.zooKeeperClusterAndNameSpace.ZooKeeperCluster;
import spring.cloud.tasks.api.utils.StatisticsTableKeyConstant;

import javax.annotation.Resource;
import java.util.List;

public class StatisticsPersistence {

    private static final Logger log = LoggerFactory.getLogger(StatisticsPersistence.class);

    @Resource
    private StatisticsDtoService statisticsDtoService;

    public void persist(ZooKeeperCluster zooKeeperCluster, StatisticsAnalyzerModel statisticsAnalyzerModel) {
        List<TaskStatistics> taskStatisticsList = statisticsAnalyzerModel.getTaskStatisticsAnalyzer().getTaskStatisticsList();
        List<ExecutorStatistics> executorStatisticsList = statisticsAnalyzerModel.getExecutorInfoAnalyzer().getExecutorList();


        // 负荷最重的Top10的任务列表
        saveOrUpdateTop10LoadJob(zooKeeperCluster.getZooKeeperAddressList(), taskStatisticsList);


        //
        // 负荷最重的Top10的Executor列表
        saveOrUpdateTop10LoadExecutor(zooKeeperCluster.getZooKeeperAddressList(), executorStatisticsList);


    }


    //


    private void saveOrUpdateTop10LoadJob(String zooKeeperAddressList, List<TaskStatistics> taskStatisticsList) {
        try {
            taskStatisticsList = DashboardServiceHelper.sortJobByLoadLevel(taskStatisticsList);
            List<TaskStatistics> top10LoadTaskStatisticsList = taskStatisticsList.subList(0, taskStatisticsList.size() > 9 ? 10 : taskStatisticsList.size());
            String top10LoadJobJsonString = JSON.toJSONString(top10LoadTaskStatisticsList);
            StatisticsDto statisticsDto = statisticsDtoService.findStatisticsByZooKeeperAddressListAndType(StatisticsTableKeyConstant.TOP_10_LOAD_JOB, zooKeeperAddressList);
            if (statisticsDto == null) {
                StatisticsDto dto = new StatisticsDto(zooKeeperAddressList, StatisticsTableKeyConstant.TOP_10_LOAD_JOB, top10LoadJobJsonString);
                statisticsDtoService.insert(dto);
            } else {
                statisticsDto.setStatistics(top10LoadJobJsonString);
                statisticsDtoService.update(statisticsDto);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }


    //Executor


    private void saveOrUpdateTop10LoadExecutor(String zooKeeperAddressList, List<ExecutorStatistics> executorStatisticsList) {
        try {
            executorStatisticsList = DashboardServiceHelper.sortExecutorByLoadLevel(executorStatisticsList);
            List<ExecutorStatistics> top10LoadExecutorStatisticsList = executorStatisticsList.subList(0, executorStatisticsList.size() > 9 ? 10 : executorStatisticsList.size());
            String top10LoadExecutorJsonString = JSON.toJSONString(top10LoadExecutorStatisticsList);
            StatisticsDto statisticsDto = statisticsDtoService.findStatisticsByZooKeeperAddressListAndType(StatisticsTableKeyConstant.TOP_10_LOAD_EXECUTOR, zooKeeperAddressList);
            if (statisticsDto == null) {
                StatisticsDto dto = new StatisticsDto(zooKeeperAddressList, StatisticsTableKeyConstant.TOP_10_LOAD_EXECUTOR, top10LoadExecutorJsonString);
                statisticsDtoService.insert(dto);
            } else {
                statisticsDto.setStatistics(top10LoadExecutorJsonString);
                statisticsDtoService.update(statisticsDto);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }


}
